﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class OutStockBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public OutStockBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string SerialNo, string OutStockID)
        {
            if (DAO.T_OUTSTOCKs.Where(t => t.SERIAL_NO == Convert.ToInt32(SerialNo)
                                        && t.OUTSTOCK_ID.ToLower() == OutStockID.ToLower()).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_OUTSTOCK> GetOutStockList()
        {
            var list = DAO.T_OUTSTOCKs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }

            return list;
        }

        public T_OUTSTOCK GetOutStockByID(string ID)
        {
            return DAO.T_OUTSTOCKs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }
        public IEnumerable<T_OUTSTOCK_ITEM> GetOutStockDetailByStockID(string ID)
        {
            var list = DAO.T_OUTSTOCK_ITEMs.Where(t => t.OUTSTOCK_ID.ToString() == ID);
            if (!string.IsNullOrEmpty(WhereCondition))
            {
                list = list.Where(WhereCondition);
            }
            if (!string.IsNullOrEmpty(OrderCondition.Trim()))
            {
                list = list.OrderBy(OrderCondition);
            }
            return list;
        }

        public int AddOutStock(T_OUTSTOCK OutStock)
        {
            try
            {
                OutStock.ID = DAO.GetGUID();

                DAO.T_OUTSTOCKs.InsertOnSubmit(OutStock);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddOutStock(T_OUTSTOCK OutStock, out string OutStockGuid)
        {
            try
            {
                OutStock.ID = DAO.GetGUID();
                
                DAO.T_OUTSTOCKs.InsertOnSubmit(OutStock);
                DAO.SubmitChanges();

                OutStockGuid = OutStock.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                OutStockGuid = "";
                return -1;
            }
        }

        public int UpdateOutStock(T_OUTSTOCK OutStock)
        {
            try
            {
                T_OUTSTOCK updateEntity = DAO.T_OUTSTOCKs.SingleOrDefault(t=>t.ID.ToString()==OutStock.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.AUDIT_TIME = OutStock.AUDIT_TIME;
                updateEntity.AUDITOR_ID = OutStock.AUDITOR_ID;
                updateEntity.CAR_NO = OutStock.CAR_NO;
                updateEntity.CLIENT_ID = OutStock.CLIENT_ID;
                updateEntity.CREATE_TIME = OutStock.CREATE_TIME;
                updateEntity.CREATOR_ID = OutStock.CREATOR_ID;
                updateEntity.OUTSTOCK_DATE = OutStock.OUTSTOCK_DATE;
                updateEntity.OUTSTOCK_ID = OutStock.OUTSTOCK_ID;
                //updateEntity.SERIAL_NO = OutStock.SERIAL_NO;
                updateEntity.STATUS = OutStock.STATUS;
                updateEntity.TRADE = OutStock.TRADE;
                updateEntity.TRANSFER = OutStock.TRANSFER;
                updateEntity.TRANSFER_NO = OutStock.TRANSFER_NO;
                updateEntity.TYPE = OutStock.TYPE;
                updateEntity.RawID = OutStock.RawID;
                updateEntity.Remark = OutStock.Remark;
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteOutStock(string ID)
        {
            try
            {
                T_OUTSTOCK deleteEntity = DAO.T_OUTSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_OUTSTOCK_ITEMs.DeleteAllOnSubmit(deleteEntity.T_OUTSTOCK_ITEMs);
                DAO.T_OUTSTOCKs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteOutStocks(List<string> IDs)
        {
            try
            {
                foreach (string ID in IDs)
                {
                    T_OUTSTOCK deleteEntity = DAO.T_OUTSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);
                    if (deleteEntity == null)
                        return -1;
                    DAO.T_OUTSTOCK_ITEMs.DeleteAllOnSubmit(deleteEntity.T_OUTSTOCK_ITEMs);
                    DAO.T_OUTSTOCKs.DeleteOnSubmit(deleteEntity);
                }
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AuditOutStock(string ID, Guid UserID,out string  Message)
        {
            try
            {
                Message = string.Empty;
                DAO.BeginTransaction();
                T_OUTSTOCK entity = DAO.T_OUTSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);

                entity.STATUS = (int)Enumerations.StockStatus.审核通过;
                entity.AUDIT_TIME = DateTime.Now;
                entity.AUDITOR_ID = UserID;

                foreach (T_OUTSTOCK_ITEM item in entity.T_OUTSTOCK_ITEMs)
                {
                    T_STORE store = DAO.T_STOREs.SingleOrDefault(t => t.PRODUCT_DETAIL_ID.ToString() == item.PRODUCT_DETAIL_ID.ToString()
                                        && t.FloorID.ToString() == item.FLOOR_ID.ToString());


                    
                    double OldGorssWeight = store.GROSS_WEIGHT;
                    double OldNet_Weight = store.NET_WEIGHT;
                    double OldPacketQty = store.PACKET_QTY;
                    double OldPacketQty2 = store.PACKET_QTY2;
                    store.GROSS_WEIGHT -= (double)item.GROSS_WEIGHT;
                    store.NET_WEIGHT -= (double)item.NET_WEIGHT;
                    store.PACKET_QTY -= (double)item.PACKET_QTY;
                    store.PACKET_QTY2 -= (double)item.PACKET_QTY2;

                    if (store.GROSS_WEIGHT < 0)
                    {
                        DAO.RollBack();
                        Message = "库存中的" + store.T_PRODUCT.NAME + "条形码为"+store.T_PRODUCT_DETAIL.BAR_CODE+"，毛重在"+store.T_FLOOR.T_STOCK_AREA.T_STOCK.STOCK_NAME+"->"+store.T_FLOOR.T_STOCK_AREA.AREA_NAME+"->"+store.T_FLOOR.FloorName+"库存只有" + Convert.ToString(OldGorssWeight)+",不足出库需要的"+item.GROSS_WEIGHT;
                        return -1;
                    }
                    if (store.NET_WEIGHT < 0)
                    {
                        DAO.RollBack();
                        Message = "库存中的" + store.T_PRODUCT.NAME + "条形码为" + store.T_PRODUCT_DETAIL.BAR_CODE + "，净重在" + store.T_FLOOR.T_STOCK_AREA.T_STOCK.STOCK_NAME + "->" + store.T_FLOOR.T_STOCK_AREA.AREA_NAME + "->" + store.T_FLOOR.FloorName + "库存只有" + Convert.ToString(OldNet_Weight) + ",不足出库需要的" + item.NET_WEIGHT;
                        return -2;
                    }
                    if (store.PACKET_QTY < 0)
                    {
                        DAO.RollBack();
                        Message = "库存中的" + store.T_PRODUCT.NAME + "条形码为" + store.T_PRODUCT_DETAIL.BAR_CODE + "，大件数在"+ store.T_FLOOR.T_STOCK_AREA.T_STOCK.STOCK_NAME + "->" + store.T_FLOOR.T_STOCK_AREA.AREA_NAME + "->" + store.T_FLOOR.FloorName +"库存只有" + Convert.ToString(OldPacketQty) + ",不足出库需要的" + item.PACKET_QTY;
                        return -3;
                    }
                    if(store.PACKET_QTY2 < 0)
                    {
                        DAO.RollBack();
                        Message = "库存中的" + store.T_PRODUCT.NAME + "条形码为" + store.T_PRODUCT_DETAIL.BAR_CODE + "，小件数在" + store.T_FLOOR.T_STOCK_AREA.T_STOCK.STOCK_NAME + "->" + store.T_FLOOR.T_STOCK_AREA.AREA_NAME + "->" + store.T_FLOOR.FloorName +"库存只有" + Convert.ToString(OldPacketQty2) + ",不足出库需要的" + item.PACKET_QTY2;
                        return -4;
                    }

                    DAO.SubmitChanges();
                }
                DAO.SubmitChanges();
                DAO.Commit();
                return 1;
            }
            catch (Exception ex)
            {
                DAO.RollBack();
                Message = ex.ToString();
                return -1;
            }
        }

        public int BackOutStock(string ID)
        {
            try
            {
                T_OUTSTOCK entity = DAO.T_OUTSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);

                entity.STATUS = (int)Enumerations.StockStatus.打回;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public bool IsAudited(string ID)
        {
            T_OUTSTOCK entity = DAO.T_OUTSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);

            return entity.STATUS == (int)Enumerations.StockStatus.审核通过 ? true : false;
        }
    }
}
